package com.example.cas.clientone.elasticsearch.controller;

/**
 * @program: casdemo
 * @description: 全文检索 控制器
 * @author: 张 桥
 * @create: 2020-08-21 15:13
 */

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.cas.clientone.elasticsearch.entity.DocBean;
import com.example.cas.clientone.elasticsearch.service.IElasticService;
import com.example.cas.clientone.system.controller.IndexController;
import com.example.cas.clientone.utils.StringUtils;
import lombok.extern.log4j.Log4j2;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/elastic")
public class ElasticController {
    private static Logger logger = LoggerFactory.getLogger(ElasticController.class);
    @Autowired
    private IElasticService elasticService;

    @GetMapping("/init")
    public String init() {
        logger.info("elastic---------------init----------------------");
        try {
            //DocBean类 索引名称
            elasticService.createIndex("ems");
            List<DocBean> list = new ArrayList<>();
            list.add(new DocBean(1L, "XX0193", "XX8064", "中国人民解放军", 1));
            list.add(new DocBean(2L, "XX0210", "XX7475", "中国人民解放军解放中国", 1));
            list.add(new DocBean(3L, "XX0257", "XX8097", "中国人民解放军解放中国人民", 1));
            elasticService.saveAll(list);
        } finally {

        }
        return "请返回,点击查询";
    }

    @GetMapping("/all")
    public Iterator<DocBean> all(@RequestParam Map param) {
        logger.info("elastic---------------all----------------------" + param.get("test"));
        return elasticService.findAll();

    }

    @PostMapping("/query")
    public String query(@RequestParam Map param) {
        logger.info("elastic---------------query----------------------" + param.get("test"));
        Page<DocBean> fistCode = elasticService.query(StringUtils.getStringByObj(param.get("test")));
        logger.info(JSON.toJSONString(fistCode));
        return JSON.toJSONString(fistCode);
//        return fistCode.getContent().get(0).getId()+fistCode.getContent().get(0).getContent();
    }

    //高亮显示
    @PostMapping("/highlight")
    public String highlight(@RequestParam Map param) {
        //配置标题高亮显示
        HighlightBuilder highlightBuilder = new HighlightBuilder(); //生成高亮查询器
        highlightBuilder.field("firstCode");      //高亮查询字段
        highlightBuilder.field("content");    //高亮查询字段
        highlightBuilder.requireFieldMatch(false);     //如果要多个字段高亮,这项要为false
        highlightBuilder.preTags("<span style=\"color:red\">");   //高亮设置
        highlightBuilder.postTags("</span>");

        //下面这两项,如果你要高亮如文字内容等有很多字的字段,必须配置,不然会导致高亮不全,文章内容缺失等
        highlightBuilder.fragmentSize(800000); //最大高亮分片数
        highlightBuilder.numOfFragments(0); //从第一个分片获取高亮片段
        return  "";
    }
}
